package com.example.user.controller;

import com.example.core.annotation.HasPerm;
import com.example.core.utils.ServletUtils;
import com.example.model.check_in.PasswordDo;
import com.example.model.response.Result;
import com.example.model.room_type.RoomType;
import com.example.model.user.User;
import com.example.user.service.UserService;
import com.example.user.utils.TokenWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenWorker tokenWorker;

    @GetMapping("/{userId}")
    public Result get(@PathVariable("userId") Long userId) {
        return Result.success(userService.get(userId));
    }

    @HasPerm("system:user:list")
    @GetMapping("/list")
    public Result list(User user) {
        return Result.success(userService.list(user));
    }
    @HasPerm("system:user:list")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, User user) {
        System.out.println(user.toString());
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.list(user);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result insert(@RequestBody User user) {
        return Result.success(userService.insert(user));
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        return Result.success(userService.update(user));
    }

    @DeleteMapping("/{userId}")
    public Result delete(@PathVariable("userId") Long userId) {
        return Result.success(userService.delete(userId));
    }

    //重置单个密码
    @PutMapping("/resetPassword")
    public Result resetPassword(@RequestBody User user) {
        return Result.success(userService.resetPassword(user));
    }

    //重置全部密码
    @PutMapping("/resetAllPasswords")
    public Result resetAllPasswords() {
        return Result.success(userService.resetAllPasswords());
    }
    @PostMapping("/insertUserRole")
    public Result insertUserRole(@RequestBody User user) {
        return Result.success(userService.insertUserRole(user));
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordDo passwordDo) {
        return userService.updatePassword(passwordDo, tokenWorker.getLoginUser(ServletUtils.getRequest())) == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }

}
